package org.traffometer.traffosim;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import junit.framework.TestCase;
import org.junit.Test;

/**
 * Provides tests for the Main class.
 * @author Mark Stillwell
 */
public class DoNotTestMain extends TestCase {
  
  private static final String uiParam = "-ui";
  /**
   * Tests ability of Main to load a valid user interface.
   * @throws Exception if there is a problem
   */
  @Test
  public void testExample() throws Exception {
    ByteArrayOutputStream testOut = new ByteArrayOutputStream();
    PrintStream saveOut = System.out;
    System.setOut(new PrintStream(testOut));
    Main.main(new String[] {"blah", uiParam, "Example"});
    System.setOut(saveOut);
    //System.err.println(testOut.toString());
    assertTrue(
      "expected output",
      testOut.toString().matches(".*\n?.*ExampleUi successfully loaded with 3 arguments.\n")
    );
  }
  
  /**
   * Tests result of trying to load a nonexistant user interface.
   * @throws Exception if there is a problem
   */
  @Test
  public void testDoesNotExist() throws Exception {
    ByteArrayOutputStream testErr = new ByteArrayOutputStream();
    PrintStream saveErr = System.err;
    System.setErr(new PrintStream(testErr));
    Main.main(new String[] {uiParam, "DoesNotExist" });
    System.setErr(saveErr);
    assertEquals("expect does not exist", "DoesNotExist user interface not found.\n", 
        testErr.toString());
  }

  /**
   * Tests result of trying to load an invalid user interface.
   * @throws Exception if there is a problem
   */
  @Test
  public void testExampleNot() throws Exception {
    ByteArrayOutputStream testErr = new ByteArrayOutputStream();
    PrintStream saveErr = System.err;
    System.setErr(new PrintStream(testErr));
    Main.main(new String[] {uiParam, "ExampleNot" });
    System.setErr(saveErr);
    assertEquals("expect is not a user interface", "ExampleNot is not a user interface.\n", 
        testErr.toString());
  }

  /**
   * Tests result of trying to load a user interface that throws an exception.
   * @throws Exception if there is a problem
   */
  @Test
  public void testException() throws Exception {
    String expectStart = "java.lang.NoSuchMethodException: " + 
    "org.traffometer.traffosim.ExampleExceptionUi.<init>([Ljava.lang.String;)";
    ByteArrayOutputStream testErr = new ByteArrayOutputStream();
    PrintStream saveErr = System.err;
    System.setErr(new PrintStream(testErr));
    Main.main(new String[] {"-ui", "ExampleException" });
    System.setErr(saveErr);
    assertEquals("expect exception", 
        expectStart, 
        testErr.toString().substring(0, expectStart.length()));
  }
}
